package eu.telecom_bretagne.RECRUT.demo;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import eu.telecom_bretagne.RECRUT.data.model.Employe;
import eu.telecom_bretagne.RECRUT.service.IServiceEmploye;

public class AppelServiceEmploye {

	public static void main(String[] args) {
		
		test_listeEmployes();
		
		test_modifierEmploye();
		test_lireEmploye();
		
		test_creerEmploye();
		test_listeEmployes();
		
		test_supprimerEmploye(1);
		test_listeEmployes();		
	
	}

	public static IServiceEmploye getServiceEmploye() {
		
		InitialContext ctx;
		IServiceEmploye serviceEmploye = null;
		try {
			
			ctx = new InitialContext();
			serviceEmploye = (IServiceEmploye) ctx.lookup(IServiceEmploye.JNDI_NAME);
		} 
		catch (NamingException e) {
			
			// Unable to retrieve the context or the service
			e.printStackTrace();
			System.exit(-1);
		}
		return serviceEmploye;
	}

	public static void test_listeEmployes() {
		for (Employe employe : getServiceEmploye().listeEmployes()) {
			System.out.println("Employe id:" + employe.getId() + " nom:"
					+ employe.getNomEmploye());
		}
	}
	
	public static void test_lireEmploye() {

		Employe employe = getServiceEmploye().lireEmploye(1);
		System.out.println("Employe id=1 nom:" + employe.getNomEmploye());
	}
	
	public static void test_modifierEmploye(){
		
		getServiceEmploye().modifierEmploye(1, "nouveau nom employe 1", "nouveau prenom employe 1", "nouveau type employe 1", "nouveau departement employe 1");
	}
	
	public static void test_creerEmploye(){
		
		getServiceEmploye().creerEmploye("Blanchard", "Gérard", "urbaniste", "DSI");
	}
	
	public static void test_supprimerEmploye(int id){
		
		getServiceEmploye().supprimerEmploye(id);
	}
	
}
